﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model.Entities;
using NHibernate;
using Totoro.ORMapping;

namespace Bussiness.ModelQuery
{
    public class ComplaintManager
    {
        public ComplaintManager()
        { 
        }
        private static ComplaintManager instance;
        public static ComplaintManager Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new ComplaintManager();
                    instance.LoadData();
                }
                return instance;
            }
        }
        Dictionary<string, Complaints> complaintCaches = new Dictionary<string, Complaints>();
        public void LoadData()
        {
            complaintCaches.Clear();
            ISession session = NHHelper.GetCurrentSession();
            ICriteria crt = session.CreateCriteria<Complaints>();
            IList<Complaints> compList = null;
            compList = crt.List<Complaints>();
            if (compList != null)
            {
                foreach (Complaints complaint in compList)
                {
                    complaintCaches.Add(complaint.ID, complaint);
                }
            }
        }
        /// <summary>
        /// 根据Id获取评论对象
        /// </summary>
        /// <param name="sId">ID</param>
        /// <returns></returns>
        public Complaints GetComplaintById(string sId)
        {
            if (complaintCaches.ContainsKey(sId))
            {
                return complaintCaches[sId];
            }
            return null;
        }
        /// <summary>
        /// 获取所有的评论
        /// </summary>
        /// <returns></returns>
        public Complaints[] GetComplaints()
        {
            return complaintCaches.Values.ToArray();
        }
        /// <summary>
        /// 获取订单的评论
        /// </summary>
        /// <param name="sOrderId">订单Id</param>
        /// <returns></returns>
        public Complaints GetComplaintByOrderId(string sOrderId)
        {
            foreach (Complaints cmp in complaintCaches.Values)
            {
                if (cmp.OrderID == sOrderId)
                {
                    return cmp;
                }
            }
            return null;
        }
        /// <summary>
        /// 获取某一类型的评论
        /// </summary>
        /// <param name="sType">类型</param>
        /// <returns></returns>
        public Complaints[] GetComplaintByType(int sType)
        {
            List<Complaints> cmpList = new List<Complaints>();
            foreach (Complaints cmp in complaintCaches.Values)
            {
                if (cmp.ComplaintType == sType)
                {
                    cmpList.Add(cmp);
                }
            }
            return cmpList.ToArray();
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sId"></param>
        public void DeleteComplaintByID(string sId)
        {
            ISession session = NHHelper.GetCurrentSession();
            Complaints cmp = GetComplaintById(sId);
            if (cmp != null)
            {
                session.Delete(cmp);
                complaintCaches.Remove(sId);
            }
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="productComment"></param>
        public void InsertComplaintComment(Complaints complaint)
        {
            NHHelper.SaveOjb(complaint);
            complaintCaches.Add(complaint.ID, complaint);
        }
    }
}
